﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SP_XHWeb.WebParts.EmployeeInfo;
using System.Collections.Generic;
using Microsoft.SharePoint;
using System.Text;
using System.Linq;

namespace SP_XHWeb.WebParts.EmployeePicture
{
    public partial class EmployeePictureUserControl : UserControl
    {
        string siteUrl = string.Empty;
        public string EmployeeInfoUrl = "/_layouts/SP_XHWeb/EmployeeDetails.aspx?item_id=";
        public string piclink = "/companyinfo/pages/yellowpage2.aspx";
        public string txtlink = "/companyinfo/pages/yellowpage.aspx";
        public string pname = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            string absolPath = this.Page.Request.Url.AbsolutePath.ToLower();


            string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            string[] str = absolPath.Substring(1).Split('/');


            if (str != null && str.Length > 2 && str[0] == "projectinfo" && str[1] != "")
            {
                using (SPSite site = new SPSite(hostUrl + "/" + str[0] + "/" + str[1]))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        pname = web.Title.Trim().Replace("项目", "");
                        txtlink = "/" + str[0] + "/" + str[1] + "/pages/employee.aspx";
                        piclink = "/" + str[0] + "/" + str[1] + "/pages/employee2.aspx";
                    }
                }
            }
            if (!IsPostBack)
            {
                if (string.IsNullOrEmpty(pname))
                {
                    ddl3.DataSource = InitCompany();
                    ddl3.DataBind();
                    gvEppic.DataSource = GetPicEmployee();
                    gvEppic.DataBind();
                }
                else
                {
                    ddl3.DataSource = InitCompany(pname);
                    ddl3.DataBind();
                    gvEppic.DataSource = GetPicEmployee(pname);
                    gvEppic.DataBind();
                }

                ddl4.DataSource = InitDepartment();
                ddl4.DataBind();


            }

        }

        private List<string> GetPicEmployee()
        {
            List<string> strList = new List<string>();
            List<List<EmployeeDetails>> allLists = GetPicEmployeeList();
            for (int i = 0; i < allLists.Count; i++)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<div class='family_portrait_sma'>");
                sb.Append("<ul>");

                for (int j = 0; j < allLists[i].Count; j++)
                {
                    sb.Append("<li>");
                    sb.AppendFormat("<a href='{0}'>{1}</a>", EmployeeInfoUrl + allLists[i][j].EID, allLists[i][j].Picture);
                    sb.Append("<span>");
                    sb.AppendFormat("<strong><a href='{0}'>{1}</a></strong><br />", EmployeeInfoUrl + allLists[i][j].EID, allLists[i][j].Name);
                    sb.AppendFormat("{0}<br />", allLists[i][j].Company);
                    sb.AppendFormat("{0}<br />", allLists[i][j].Department);
                    sb.AppendFormat("{0}<br />", allLists[i][j].Position);
                    sb.Append("</span>");
                    sb.Append("</li>");
                }


                sb.Append("</ul>");
                sb.Append("</div>");

                strList.Add(sb.ToString());
            }


            return strList;
        }

        private List<string> GetPicEmployee(string name)
        {
            List<string> strList = new List<string>();
            List<List<EmployeeDetails>> allLists = GetPicEmployeeList(name);
            for (int i = 0; i < allLists.Count; i++)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<div class='family_portrait_sma'>");
                sb.Append("<ul>");

                for (int j = 0; j < allLists[i].Count; j++)
                {
                    sb.Append("<li>");
                    sb.AppendFormat("<a href='{0}'>{1}</a>", EmployeeInfoUrl + allLists[i][j].EID, allLists[i][j].Picture);
                    sb.Append("<span>");
                    sb.AppendFormat("<strong><a href='{0}'>{1}</a></strong><br />", EmployeeInfoUrl + allLists[i][j].EID, allLists[i][j].Name);
                    sb.AppendFormat("{0}<br />", allLists[i][j].Company);
                    sb.AppendFormat("{0}<br />", allLists[i][j].Department);
                    sb.AppendFormat("{0}<br />", allLists[i][j].Position);
                    sb.Append("</span>");
                    sb.Append("</li>");
                }


                sb.Append("</ul>");
                sb.Append("</div>");

                strList.Add(sb.ToString());
            }


            return strList;
        }


        private List<string> GetPicEmployee(string strComp, string strDept, string strName)
        {
            List<string> strList = new List<string>();
            List<List<EmployeeDetails>> allLists = GetPicEmployeeList(strComp, strDept, strName);
            for (int i = 0; i < allLists.Count; i++)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<div class='family_portrait_sma'>");
                sb.Append("<ul>");

                for (int j = 0; j < allLists[i].Count; j++)
                {
                    sb.Append("<li>");
                    sb.AppendFormat("<a href='{0}'>{1}</a>", EmployeeInfoUrl + allLists[i][j].EID, allLists[i][j].Picture);
                    sb.Append("<span>");
                    sb.AppendFormat("<strong><a href='{0}'>{1}</a></strong><br />", EmployeeInfoUrl + allLists[i][j].EID, allLists[i][j].Name);
                    sb.AppendFormat("{0}<br />", allLists[i][j].Company);
                    sb.AppendFormat("{0}<br />", allLists[i][j].Department);
                    sb.AppendFormat("{0}<br />", allLists[i][j].Position);
                    sb.Append("</span>");
                    sb.Append("</li>");
                }


                sb.Append("</ul>");
                sb.Append("</div>");

                strList.Add(sb.ToString());
            }


            return strList;
        }

        private List<List<EmployeeDetails>> GetPicEmployeeList()
        {
            List<List<EmployeeDetails>> allLists = new List<List<EmployeeDetails>>();
            string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            if (!string.IsNullOrEmpty(hostUrl))
            {
                using (SPSite site = new SPSite(hostUrl + "/companyinfo/default.aspx"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists.TryGetList("EmployeeInfo");
                        if (list != null && list.ItemCount != 0)
                        {
                            List<EmployeeDetails> temlist = new List<EmployeeDetails>();
                            foreach (SPListItem item in list.Items)
                            {
                                temlist.Add(new EmployeeDetails(item.ID, item["姓名"].ToString().Trim(), item["照片"] != null && !string.IsNullOrEmpty(item["照片"].ToString()) ? item["照片"].ToString().Trim().Replace("<br/>", "").Replace("<br />", "").Replace("<p>", "").Replace("</p>", "") : "", item["性别"].ToString(), item["公司"].ToString().Trim(), item["部门"].ToString().Trim(), item["职位"].ToString().Trim(), item["固定电话"].ToString().Trim(), item["移动电话"].ToString().Trim(), item["电子邮件"].ToString(), item["生日"].ToString().Trim(), item["新浪微博"].ToString(), item["QQ"].ToString(), item["MSN"].ToString(), item["个人博客地址"].ToString(), item["自我介绍"] != null && !string.IsNullOrEmpty(item["自我介绍"].ToString()) ? item["自我介绍"].ToString() : "", item["兴趣爱好"] != null && !string.IsNullOrEmpty(item["兴趣爱好"].ToString()) ? item["兴趣爱好"].ToString() : "", item["入职日期"].ToString(), item["排序数"] != null && !string.IsNullOrEmpty(item["排序数"].ToString()) ? Int32.Parse(item["排序数"].ToString()) : 9999));
                            }
                            if (temlist != null && temlist.Count > 0)
                            {
                                temlist = (from listemp in temlist orderby listemp.OrderNum ascending select listemp).ToList<EmployeeDetails>();

                                List<EmployeeDetails> lists = new List<EmployeeDetails>();
                                int rowCount = 0;
                                //foreach (SPListItem item in list.Items)
                                foreach (EmployeeDetails empitem in temlist)
                                {
                                    rowCount++;
                                    //lists.Add(new EmployeeDetails(item.ID, item["姓名"].ToString().Trim(), item["照片"] != null && !string.IsNullOrEmpty(item["照片"].ToString()) ? item["照片"].ToString().Trim().Replace("<br/>", "").Replace("<br />", "").Replace("<p>", "").Replace("</p>", "") : "", item["性别"].ToString(), item["公司"].ToString().Trim(), item["部门"].ToString().Trim(), item["职位"].ToString().Trim(), item["固定电话"].ToString().Trim(), item["移动电话"].ToString().Trim(), item["电子邮件"].ToString(), item["生日"].ToString().Trim(), item["新浪微博"].ToString(), item["QQ"].ToString(), item["MSN"].ToString(), item["个人博客地址"].ToString(), item["自我介绍"] != null && !string.IsNullOrEmpty(item["自我介绍"].ToString()) ? item["自我介绍"].ToString() : "", item["兴趣爱好"] != null && !string.IsNullOrEmpty(item["兴趣爱好"].ToString()) ? item["兴趣爱好"].ToString() : "", item["入职日期"].ToString(), item["排序数"] != null && !string.IsNullOrEmpty(item["排序数"].ToString()) ? Int32.Parse(item["排序数"].ToString()) : 9999));
                                    lists.Add(empitem);
                                    if (lists.Count == 3)
                                    {
                                        if (lists != null && lists.Count > 0)
                                        {
                                            lists = (from emp in lists orderby emp.OrderNum ascending select emp).ToList<EmployeeDetails>();
                                        }
                                        allLists.Add(lists);
                                        lists = new List<EmployeeDetails>();
                                        continue;
                                    }
                                    if (rowCount == list.ItemCount)
                                    {
                                        if (lists != null && lists.Count > 0)
                                        {
                                            lists = (from emp in lists orderby emp.OrderNum ascending select emp).ToList<EmployeeDetails>();
                                        }
                                        allLists.Add(lists);
                                    }

                                }
                            }
                        }
                    }
                }
            }
            return allLists;
        }

        private List<List<EmployeeDetails>> GetPicEmployeeList(string name)
        {
            List<List<EmployeeDetails>> allLists = new List<List<EmployeeDetails>>();
            string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            if (!string.IsNullOrEmpty(hostUrl))
            {
                using (SPSite site = new SPSite(hostUrl + "/companyinfo/default.aspx"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists.TryGetList("EmployeeInfo");
                        if (list != null && list.ItemCount != 0)
                        {
                            List<EmployeeDetails> temlist = new List<EmployeeDetails>();
                            foreach (SPListItem item in list.Items)
                            {
                                temlist.Add(new EmployeeDetails(item.ID, item["姓名"].ToString().Trim(), item["照片"] != null && !string.IsNullOrEmpty(item["照片"].ToString()) ? item["照片"].ToString().Trim().Replace("<br/>", "").Replace("<br />", "").Replace("<p>", "").Replace("</p>", "") : "", item["性别"].ToString(), item["公司"].ToString().Trim(), item["部门"].ToString().Trim(), item["职位"].ToString().Trim(), item["固定电话"].ToString().Trim(), item["移动电话"].ToString().Trim(), item["电子邮件"].ToString(), item["生日"].ToString().Trim(), item["新浪微博"].ToString(), item["QQ"].ToString(), item["MSN"].ToString(), item["个人博客地址"].ToString(), item["自我介绍"] != null && !string.IsNullOrEmpty(item["自我介绍"].ToString()) ? item["自我介绍"].ToString() : "", item["兴趣爱好"] != null && !string.IsNullOrEmpty(item["兴趣爱好"].ToString()) ? item["兴趣爱好"].ToString() : "", item["入职日期"].ToString(), item["排序数"] != null && !string.IsNullOrEmpty(item["排序数"].ToString()) ? Int32.Parse(item["排序数"].ToString()) : 9999));
                            }
                            if (temlist != null && temlist.Count > 0)
                            {
                                temlist = (from listemp in temlist orderby listemp.OrderNum ascending select listemp).ToList<EmployeeDetails>();


                                List<EmployeeDetails> lists = new List<EmployeeDetails>();
                                int rowCount = 0;
                                //foreach (SPListItem item in list.Items)
                                foreach (EmployeeDetails empitem in temlist)
                                {
                                    rowCount++;
                                    //if (item["公司"].ToString().Trim().StartsWith(name))
                                    if (empitem.Company.Trim().StartsWith(name))
                                    {

                                        //lists.Add(new EmployeeDetails(item.ID, item["姓名"].ToString().Trim(), item["照片"] != null && !string.IsNullOrEmpty(item["照片"].ToString()) ? item["照片"].ToString().Trim().Replace("<br/>", "").Replace("<br />", "").Replace("<p>", "").Replace("</p>", "") : "", item["性别"].ToString(), item["公司"].ToString().Trim(), item["部门"].ToString().Trim(), item["职位"].ToString().Trim(), item["固定电话"].ToString().Trim(), item["移动电话"].ToString().Trim(), item["电子邮件"].ToString(), item["生日"].ToString().Trim(), item["新浪微博"].ToString(), item["QQ"].ToString(), item["MSN"].ToString(), item["个人博客地址"].ToString(), item["自我介绍"] != null && !string.IsNullOrEmpty(item["自我介绍"].ToString()) ? item["自我介绍"].ToString() : "", item["兴趣爱好"] != null && !string.IsNullOrEmpty(item["兴趣爱好"].ToString()) ? item["兴趣爱好"].ToString() : "", item["入职日期"].ToString(), item["排序数"] != null && !string.IsNullOrEmpty(item["排序数"].ToString()) ? Int32.Parse(item["排序数"].ToString()) : 9999));
                                        lists.Add(empitem);
                                        if (lists.Count == 3)
                                        {
                                            if (lists != null && lists.Count > 0)
                                            {
                                                lists = (from emp in lists orderby emp.OrderNum ascending select emp).ToList<EmployeeDetails>();
                                            }
                                            allLists.Add(lists);
                                            lists = new List<EmployeeDetails>();
                                            continue;
                                        }
                                    }
                                    if (rowCount == list.ItemCount)
                                    {
                                        if (lists != null && lists.Count > 0)
                                        {
                                            lists = (from emp in lists orderby emp.OrderNum ascending select emp).ToList<EmployeeDetails>();
                                        }
                                        allLists.Add(lists);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return allLists;
        }

        private List<List<EmployeeDetails>> GetPicEmployeeList(string strComp, string strDept, string strName)
        {
            List<List<EmployeeDetails>> allLists = new List<List<EmployeeDetails>>();
            string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            if (!string.IsNullOrEmpty(hostUrl))
            {
                using (SPSite site = new SPSite(hostUrl + "/companyinfo/default.aspx"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists.TryGetList("EmployeeInfo");
                        if (list != null && list.ItemCount != 0)
                        {
                            List<EmployeeDetails> lists = new List<EmployeeDetails>();
                            //公司不为空
                            if (strComp != "选择公司" && strDept == "选择部门" && string.IsNullOrEmpty(strName))
                            {
                                foreach (SPListItem item in list.Items)
                                {
                                    if (item["公司"].ToString().Trim().ToLower() == strComp.Trim().ToLower())
                                    {
                                        lists.Add(new EmployeeDetails(item.ID, item["姓名"].ToString().Trim(), item["照片"] != null && !string.IsNullOrEmpty(item["照片"].ToString()) ? item["照片"].ToString().Trim().Replace("<br/>", "").Replace("<br />", "").Replace("<p>", "").Replace("</p>", "") : "", item["性别"].ToString(), item["公司"].ToString().Trim(), item["部门"].ToString().Trim(), item["职位"].ToString().Trim(), item["固定电话"].ToString().Trim(), item["移动电话"].ToString().Trim(), item["电子邮件"].ToString(), item["生日"].ToString().Trim(), item["新浪微博"].ToString(), item["QQ"].ToString(), item["MSN"].ToString(), item["个人博客地址"].ToString(), item["自我介绍"] != null && !string.IsNullOrEmpty(item["自我介绍"].ToString()) ? item["自我介绍"].ToString() : "", item["兴趣爱好"] != null && !string.IsNullOrEmpty(item["兴趣爱好"].ToString()) ? item["兴趣爱好"].ToString() : "", item["入职日期"].ToString(), item["排序数"] != null && !string.IsNullOrEmpty(item["排序数"].ToString()) ? Int32.Parse(item["排序数"].ToString()) : 9999));
                                    }
                                }
                            }
                            //部门不为空
                            else if (strComp == "选择公司" && strDept != "选择部门" && string.IsNullOrEmpty(strName))
                            {
                                foreach (SPListItem item in list.Items)
                                {
                                    if (item["部门"].ToString().Trim().ToLower() == strDept.Trim().ToLower())
                                    {
                                        lists.Add(new EmployeeDetails(item.ID, item["姓名"].ToString().Trim(), item["照片"] != null && !string.IsNullOrEmpty(item["照片"].ToString()) ? item["照片"].ToString().Trim().Replace("<br/>", "").Replace("<br />", "").Replace("<p>", "").Replace("</p>", "") : "", item["性别"].ToString(), item["公司"].ToString().Trim(), item["部门"].ToString().Trim(), item["职位"].ToString().Trim(), item["固定电话"].ToString().Trim(), item["移动电话"].ToString().Trim(), item["电子邮件"].ToString(), item["生日"].ToString().Trim(), item["新浪微博"].ToString(), item["QQ"].ToString(), item["MSN"].ToString(), item["个人博客地址"].ToString(), item["自我介绍"] != null && !string.IsNullOrEmpty(item["自我介绍"].ToString()) ? item["自我介绍"].ToString() : "", item["兴趣爱好"] != null && !string.IsNullOrEmpty(item["兴趣爱好"].ToString()) ? item["兴趣爱好"].ToString() : "", item["入职日期"].ToString(), item["排序数"] != null && !string.IsNullOrEmpty(item["排序数"].ToString()) ? Int32.Parse(item["排序数"].ToString()) : 9999));
                                    }
                                }
                            }
                            //姓名不为空
                            else if (strComp == "选择公司" && strDept == "选择部门" && !string.IsNullOrEmpty(strName))
                            {
                                foreach (SPListItem item in list.Items)
                                {
                                    if (item["姓名"].ToString().Trim().ToLower().Contains(strName.Trim().ToLower()))
                                    {
                                        lists.Add(new EmployeeDetails(item.ID, item["姓名"].ToString().Trim(), item["照片"] != null && !string.IsNullOrEmpty(item["照片"].ToString()) ? item["照片"].ToString().Trim().Replace("<br/>", "").Replace("<br />", "").Replace("<p>", "").Replace("</p>", "") : "", item["性别"].ToString(), item["公司"].ToString().Trim(), item["部门"].ToString().Trim(), item["职位"].ToString().Trim(), item["固定电话"].ToString().Trim(), item["移动电话"].ToString().Trim(), item["电子邮件"].ToString(), item["生日"].ToString().Trim(), item["新浪微博"].ToString(), item["QQ"].ToString(), item["MSN"].ToString(), item["个人博客地址"].ToString(), item["自我介绍"] != null && !string.IsNullOrEmpty(item["自我介绍"].ToString()) ? item["自我介绍"].ToString() : "", item["兴趣爱好"] != null && !string.IsNullOrEmpty(item["兴趣爱好"].ToString()) ? item["兴趣爱好"].ToString() : "", item["入职日期"].ToString(), item["排序数"] != null && !string.IsNullOrEmpty(item["排序数"].ToString()) ? Int32.Parse(item["排序数"].ToString()) : 9999));
                                    }
                                }
                            }
                            //公司与部门不为空
                            else if (strComp != "选择公司" && strDept != "选择部门" && string.IsNullOrEmpty(strName))
                            {
                                foreach (SPListItem item in list.Items)
                                {
                                    if (item["公司"].ToString().Trim().ToLower() == strComp.Trim().ToLower() && item["部门"].ToString().Trim().ToLower() == strDept.Trim().ToLower())
                                    {
                                        lists.Add(new EmployeeDetails(item.ID, item["姓名"].ToString().Trim(), item["照片"] != null && !string.IsNullOrEmpty(item["照片"].ToString()) ? item["照片"].ToString().Trim().Replace("<br/>", "").Replace("<br />", "").Replace("<p>", "").Replace("</p>", "") : "", item["性别"].ToString(), item["公司"].ToString().Trim(), item["部门"].ToString().Trim(), item["职位"].ToString().Trim(), item["固定电话"].ToString().Trim(), item["移动电话"].ToString().Trim(), item["电子邮件"].ToString(), item["生日"].ToString().Trim(), item["新浪微博"].ToString(), item["QQ"].ToString(), item["MSN"].ToString(), item["个人博客地址"].ToString(), item["自我介绍"] != null && !string.IsNullOrEmpty(item["自我介绍"].ToString()) ? item["自我介绍"].ToString() : "", item["兴趣爱好"] != null && !string.IsNullOrEmpty(item["兴趣爱好"].ToString()) ? item["兴趣爱好"].ToString() : "", item["入职日期"].ToString(), item["排序数"] != null && !string.IsNullOrEmpty(item["排序数"].ToString()) ? Int32.Parse(item["排序数"].ToString()) : 9999));
                                    }
                                }
                            }
                            //公司与姓名不为空
                            else if (strComp != "选择公司" && strDept == "选择部门" && !string.IsNullOrEmpty(strName))
                            {
                                foreach (SPListItem item in list.Items)
                                {
                                    if (item["公司"].ToString().Trim().ToLower() == strComp.Trim().ToLower() && item["姓名"].ToString().Trim().ToLower().Contains(strName.Trim().ToLower()))
                                    {
                                        lists.Add(new EmployeeDetails(item.ID, item["姓名"].ToString().Trim(), item["照片"] != null && !string.IsNullOrEmpty(item["照片"].ToString()) ? item["照片"].ToString().Trim().Replace("<br/>", "").Replace("<br />", "").Replace("<p>", "").Replace("</p>", "") : "", item["性别"].ToString(), item["公司"].ToString().Trim(), item["部门"].ToString().Trim(), item["职位"].ToString().Trim(), item["固定电话"].ToString().Trim(), item["移动电话"].ToString().Trim(), item["电子邮件"].ToString(), item["生日"].ToString().Trim(), item["新浪微博"].ToString(), item["QQ"].ToString(), item["MSN"].ToString(), item["个人博客地址"].ToString(), item["自我介绍"] != null && !string.IsNullOrEmpty(item["自我介绍"].ToString()) ? item["自我介绍"].ToString() : "", item["兴趣爱好"] != null && !string.IsNullOrEmpty(item["兴趣爱好"].ToString()) ? item["兴趣爱好"].ToString() : "", item["入职日期"].ToString(), item["排序数"] != null && !string.IsNullOrEmpty(item["排序数"].ToString()) ? Int32.Parse(item["排序数"].ToString()) : 9999));
                                    }
                                }
                            }
                            //部门与姓名不为空
                            else if (strComp == "选择公司" && strDept != "选择部门" && !string.IsNullOrEmpty(strName))
                            {
                                foreach (SPListItem item in list.Items)
                                {
                                    if (item["部门"].ToString().Trim().ToLower() == strDept.Trim().ToLower() && item["姓名"].ToString().Trim().ToLower().Contains(strName.Trim().ToLower()))
                                    {
                                        lists.Add(new EmployeeDetails(item.ID, item["姓名"].ToString().Trim(), item["照片"] != null && !string.IsNullOrEmpty(item["照片"].ToString()) ? item["照片"].ToString().Trim().Replace("<br/>", "").Replace("<br />", "").Replace("<p>", "").Replace("</p>", "") : "", item["性别"].ToString(), item["公司"].ToString().Trim(), item["部门"].ToString().Trim(), item["职位"].ToString().Trim(), item["固定电话"].ToString().Trim(), item["移动电话"].ToString().Trim(), item["电子邮件"].ToString(), item["生日"].ToString().Trim(), item["新浪微博"].ToString(), item["QQ"].ToString(), item["MSN"].ToString(), item["个人博客地址"].ToString(), item["自我介绍"] != null && !string.IsNullOrEmpty(item["自我介绍"].ToString()) ? item["自我介绍"].ToString() : "", item["兴趣爱好"] != null && !string.IsNullOrEmpty(item["兴趣爱好"].ToString()) ? item["兴趣爱好"].ToString() : "", item["入职日期"].ToString(), item["排序数"] != null && !string.IsNullOrEmpty(item["排序数"].ToString()) ? Int32.Parse(item["排序数"].ToString()) : 9999));
                                    }
                                }
                            }
                            //全不为空
                            else if (strComp != "选择公司" && strDept != "选择部门" && !string.IsNullOrEmpty(strName))
                            {
                                foreach (SPListItem item in list.Items)
                                {
                                    if (item["公司"].ToString().Trim().ToLower() == strComp.Trim().ToLower() && item["部门"].ToString().Trim().ToLower() == strDept.Trim().ToLower() && item["姓名"].ToString().Trim().ToLower().Contains(strName.Trim().ToLower()))
                                    {
                                        lists.Add(new EmployeeDetails(item.ID, item["姓名"].ToString().Trim(), item["照片"] != null && !string.IsNullOrEmpty(item["照片"].ToString()) ? item["照片"].ToString().Trim().Replace("<br/>", "").Replace("<br />", "").Replace("<p>", "").Replace("</p>", "") : "", item["性别"].ToString(), item["公司"].ToString().Trim(), item["部门"].ToString().Trim(), item["职位"].ToString().Trim(), item["固定电话"].ToString().Trim(), item["移动电话"].ToString().Trim(), item["电子邮件"].ToString(), item["生日"].ToString().Trim(), item["新浪微博"].ToString(), item["QQ"].ToString(), item["MSN"].ToString(), item["个人博客地址"].ToString(), item["自我介绍"] != null && !string.IsNullOrEmpty(item["自我介绍"].ToString()) ? item["自我介绍"].ToString() : "", item["兴趣爱好"] != null && !string.IsNullOrEmpty(item["兴趣爱好"].ToString()) ? item["兴趣爱好"].ToString() : "", item["入职日期"].ToString(), item["排序数"] != null && !string.IsNullOrEmpty(item["排序数"].ToString()) ? Int32.Parse(item["排序数"].ToString()) : 9999));

                                    }
                                }
                            }
                            //全为空
                            else
                            {
                                return GetPicEmployeeList();
                            }
                            if (lists != null && lists.Count != 0)
                            {
                                lists = (from emp in lists orderby emp.OrderNum ascending select emp).ToList<EmployeeDetails>();
                                if (lists.Count > 3)
                                {
                                    for (int i = 0; i < lists.Count; i += 3)
                                    {
                                        List<EmployeeDetails> tempList = new List<EmployeeDetails>();
                                        try
                                        {
                                            if (lists.Contains(lists[i]))
                                            {
                                                tempList.Add(lists[i]);
                                            }
                                            if (lists.Contains(lists[i + 1]))
                                            {
                                                tempList.Add(lists[i + 1]);
                                            }
                                            if (lists.Contains(lists[i + 2]))
                                            {
                                                tempList.Add(lists[i + 2]);
                                            }
                                        }
                                        catch
                                        {
                                        }
                                        if (tempList != null && tempList.Count > 0)
                                        {
                                            tempList = (from emp in tempList orderby emp.OrderNum ascending select emp).ToList<EmployeeDetails>();
                                        }
                                        allLists.Add(tempList);
                                    }
                                }
                                else
                                {
                                    if (lists != null && lists.Count > 0)
                                    {
                                        lists = (from emp in lists orderby emp.OrderNum ascending select emp).ToList<EmployeeDetails>();
                                    }
                                    allLists.Add(lists);
                                }
                            }
                        }
                    }
                }
            }
            return allLists;
        }

        //防止GridView翻页时报错
        protected void gvEppic_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvEppic.PageIndex = e.NewPageIndex;

            string strComp = ddl3.SelectedItem.Text.Trim();
            string strDept = ddl4.SelectedItem.Text.Trim();
            string strName = txt2.Text.Trim();
            gvEppic.DataSource = GetPicEmployee(strComp, strDept, strName);
            gvEppic.DataBind();

        }


        //全家福查询
        protected void imb2_Click(object sender, ImageClickEventArgs e)
        {
            string strComp = ddl3.SelectedItem.Text.Trim();
            string strDept = ddl4.SelectedItem.Text.Trim();
            string strName = txt2.Text.Trim();
            if (strComp == "选择公司" && strDept == "选择部门" && string.IsNullOrEmpty(strName))
            {
                List<string> listes = GetPicEmployee();
                gvEppic.DataSource = listes;
                if (listes != null && listes.Count > 0)
                {
                    gvEppic.PageIndex = 0;
                }
                gvEppic.DataBind();
                
            }
            else
            {
                List<string> lists = GetPicEmployee(strComp, strDept, strName);
                if (lists != null && lists.Count != 0)
                {
                    gvEppic.DataSource = lists;
                    if (lists != null && lists.Count > 0)
                    {
                        gvEppic.PageIndex = 0;
                    }
                    gvEppic.DataBind();
                    
                }
                else
                {
                    gvEppic.DataSource = null;
                    gvEppic.DataBind();
                }
            }
        }

        private List<string> InitCompany()
        {
            List<string> comps = new List<string>();
            comps.Add("选择公司");
            string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            if (!string.IsNullOrEmpty(hostUrl))
            {
                using (SPSite site = new SPSite(hostUrl + "/companyinfo"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists.TryGetList("EmployeeInfo");
                        if (list != null && list.ItemCount != 0)
                        {
                            foreach (SPListItem item in list.Items)
                            {
                                if (!comps.Contains(item["公司"].ToString().Trim()))
                                {
                                    comps.Add(item["公司"].ToString().Trim());
                                }
                            }
                        }
                    }
                }
            }
            return comps;
        }

        private List<string> InitCompany(string name)
        {
            List<string> comps = new List<string>();
            //comps.Add("选择公司");
            string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            if (!string.IsNullOrEmpty(hostUrl))
            {
                using (SPSite site = new SPSite(hostUrl + "/companyinfo"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists.TryGetList("EmployeeInfo");
                        if (list != null && list.ItemCount != 0)
                        {
                            foreach (SPListItem item in list.Items)
                            {
                                if (item["公司"].ToString().Trim().StartsWith(name))
                                {
                                    if (!comps.Contains(item["公司"].ToString().Trim()))
                                    {
                                        comps.Add(item["公司"].ToString().Trim());
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return comps;
        }

        private List<string> InitDepartment()
        {
            List<string> depts = new List<string>();
            depts.Add("选择部门");
            string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            if (!string.IsNullOrEmpty(hostUrl))
            {
                using (SPSite site = new SPSite(hostUrl + "/companyinfo"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists.TryGetList("EmployeeInfo");
                        if (list != null && list.ItemCount != 0)
                        {
                            foreach (SPListItem item in list.Items)
                            {
                                if (!depts.Contains(item["部门"].ToString().Trim()))
                                {
                                    depts.Add(item["部门"].ToString().Trim());
                                }
                            }
                        }
                    }
                }
            }
            return depts;
        }


    }
}

